package com.dpj;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class ScrollImage extends Activity {
	String url = "http://59.124.34.13/solomo/services/jsonptest.aspx";
	String type = "O41";
	
	LinearLayout viewGroup;
	
	int size = 5;
	int screenWidth;
	
	int imageWidth;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scroll_image);
		new GetData().execute();
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		imageWidth = screenWidth / 3;
		viewGroup = (LinearLayout) findViewById(R.id.viewGroup);
		
		
		for(int i = 0; i < size; i++) {
			ImageView iv = new ImageView(this);
			LayoutParams lp = new LayoutParams(imageWidth, 300);
			iv.setLayoutParams(lp);
			iv.setBackgroundResource(R.drawable.test);
			viewGroup.addView(iv);
		}
		
		if(size <= 2) {
			android.widget.FrameLayout.LayoutParams lp1 = new android.widget.FrameLayout.LayoutParams(android.widget.FrameLayout.LayoutParams.FILL_PARENT, android.widget.FrameLayout.LayoutParams.WRAP_CONTENT);
			lp1.gravity = Gravity.CENTER;
//			viewGroup.setGravity(Gravity.CENTER);
			viewGroup.setLayoutParams(lp1);
		}
		
	}

	class GetData extends AsyncTask<Void, Void, Void> {
		
		@Override
		protected Void doInBackground(Void... params1) {
			HttpClient client = new DefaultHttpClient();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			NameValuePair pa = new BasicNameValuePair("id",type);
			params.add(pa);
			HttpPost post = new HttpPost(url);
			try {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
						params, HTTP.UTF_8);
				post.setEntity(entity);
				HttpResponse response = client.execute(post);
				if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					String result = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
					Log.i("aac", result);
				} 
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
	}
	
}
